<?php

// $ig tells if the user is in game (ig) or not
// $user is an array that contains user information

function ryzom_app_authenticate() {

	global $ig, $user;

    // ingame or not?
	$ig = strstr($_SERVER['HTTP_USER_AGENT'], 'Ryzom');

	if(!$ig) session_start();

	if($ig || !isset($_SESSION['name']) || (ryzom_get_param('name') != ''  &&  $_SESSION['name'] != ryzom_get_param('name'))) {

		$fn = "http://atys.ryzom.com/api/player_auth.php?shardid=".ryzom_get_param('shardid')."&name=".ryzom_get_param('name')."&cid=".ryzom_get_param('cid')."&authkey=".ryzom_get_param('authkey');
		if(!$ig) $fn .= '&authserver=http://'.$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'];
		$res = file_get_contents($fn);

		if($res != '1') {
			header('Location: http://atys.ryzom.com/start/index.php');
			exit;
			//echo "<p>fn $fn</p>";
			//echo "<p>res $res</p>";
			//die('<p>bad auth</p>');
		}

		if(!$ig) {
			$_SESSION['name'] = ryzom_get_param('name');
			$_SESSION['cid'] = ryzom_get_param('cid');
			$_SESSION['shardid'] = ryzom_get_param('shardid');
			$_SESSION['lang'] = ryzom_get_param('lang');
		}

		$user['name'] = ryzom_get_param('name');
		$user['cid'] = ryzom_get_param('cid');
		$user['lang'] = ryzom_get_param('lang');

		$shards = array(101 => 'aniro', 102 => 'leanon', 103 => 'arispotle', 301 => 'aniro');
		$user['shard'] = $shards[ryzom_get_param('shardid')];
		$user['shardid'] = ryzom_get_param('shardid');

	} else if(isset($_SESSION['name'])) {

		$user['name'] = $_SESSION['name'];
		$user['shardid'] = $_SESSION['shardid'];
		$user['cid'] = $_SESSION['cid'];
		$user['lang'] = $_SESSION['lang'];
		$shards = array(101 => 'aniro', 102 => 'leanon', 103 => 'arispotle');
		$user['shard'] = $shards[$_SESSION['shardid']];

	}

	$l = ryzom_get_param('language');
	if(!$ig && $l != '') {
		$_SESSION['lang'] = $l;
		$user['lang'] = $l;
	}

	$l = ryzom_get_param('lang');
	if(!$ig && $l != '') {
		$_SESSION['lang'] = $l;
		$user['lang'] = $l;
	}

}

function ryzom_app_render($title, $content) {
	global $ig, $user;

	// Render header

	echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">';
	echo '<html><head>';
	echo '<title>'._t($title).'</title>';
	echo '<meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8" />';

	if(!$ig) {
		echo ryzom_render_header();
		echo ryzom_render_header_www();
        echo '<style type="text/css">
            .ryzom-ui-t .ryzom-ui-text-button {
                font-size: 85%;
            }
            a.ryzom-ui-text-button, a:visited.ryzom-ui-text-button {
                color:#FFFF11;
                text-decoration:none;
            }
            .ryzom-ui-text-button {
                border-radius: 4px;
                -moz-border-radius: 4px;
                -webkit-border-radius: 4px;
            }
            .ryzom-ui-text-button {
                padding: 0;margin: 0;
                border-color: #ff8 #ff3 #ff3 #ff8;
                border-width: 1px;
                border-style: solid;
                padding: 0px 4px;
                color: white;
            }
           </style>';
	}

	echo '</head><body>';

	// Render content

	if($ig)
		echo $content;
	else
		echo ryzom_render_www(ryzom_render_window('<span style="float:right;margin-right:12px;"><a href="http://atys.ryzom.com/start/index.php" class="ryzom-ui-text-button">'._t('home').'</a></span>'._t($title), $content));

	echo '</body></html>';
}

// Translate the $id in the user language
function _t($id, $args=array()) {
	global $ryzom_texts, $user;
	if(!isset($ryzom_texts[$id])) return '{'.$id.'}';
	if(!isset($ryzom_texts[$id][$user['lang']])) return '{'.$id.'['.$user['lang'].']}';
	return vsprintf($ryzom_texts[$id][$user['lang']], $args);
}

// Get a human and translated readable time, for example "3 days ago"
function ryzom_relative_time($timestamp) {
	global $ryzom_periods, $user;
	$difference = time() - $timestamp;
	$lengths = array("60","60","24","7","4.35","12","10");

	if ($difference > 0) { // this was in the past
		$ending = _t('ago');
	} else { // this was in the future
		$difference = -$difference;
		$ending = _t('to_go');
	}
	for($j = 0; $difference >= $lengths[$j]; $j++)
		$difference /= $lengths[$j];
	$difference = round($difference);
	$final = $ryzom_periods[$user['lang']][$j];
	if($difference != 1) $final .= "s";
	$text = _t('date_format', array($difference, $final, $ending));
	return $text;
}

// Always use this function to get param because in game, the param can be pass by _GET or by _POST
function ryzom_get_param($var)
{
	if (isset($_GET[$var]))
		return $_GET[$var];
	else if (isset($_POST[$var]))
		return $_POST[$var];
	else
		return '';
}

?>